﻿Public Class PhongBan
    Public st As Boolean = False
    Public manv As String = ""
    Private data As New DataTable
    Private xlpb As XLPhongBan
    Private status As Boolean = False
    Private dsnv As New List(Of DTO_NhanVienCT.NhanVien)
    Private dlieu As String
    Private vt As Integer = 0
    Private Sub bt_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_them.Click
        If tb_tenphong.Text = "" And tb_truongphong.Text = "" Then
            MessageBox.Show("Dữ Liệu Bị Sai", "Thông Báo", MessageBoxButtons.OK)
        Else
            Dim dr As DataRow = data.NewRow()
            Dim pb As New DTO_PhongBan.PhongBan
            xlpb = New XLPhongBan
            If LayDLieu(pb, 1) = False Then
                dr("MaPB") = pb.MaPB
                dr("TenPB") = pb.TenPhong
                dr("MaTRP") = manv
                dr("NgayNC") = pb.NgayNChuc
                data.Rows.Add(dr)
                dh(dr)
            Else
                MessageBox.Show("Sai dữ liệu nhập vào", "Thông báo", MessageBoxButtons.OK)
            End If
            Refesh_()
        End If
    End Sub

    Private Sub bt_sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_sua.Click
        If status = True Then
            For Each dr As DataRow In data.Rows
                If dr("MaPB") = dlieu Then
                    Dim pb As New DTO_PhongBan.PhongBan
                    If LayDLieu(pb, 0) = False Then
                        dr("MaPB") = pb.MaPB
                        dr("TenPB") = pb.TenPhong
                        dr("MaTRP") = manv
                        dr("NgayNC") = pb.NgayNChuc
                        lvphongban.Items(vt).SubItems(1).Text = pb.MaPB ' cap nhat lai trên listview
                        lvphongban.Items(vt).SubItems(2).Text = pb.TenPhong
                        lvphongban.Items(vt).SubItems(3).Text = pb.TruongPhong
                        lvphongban.Items(vt).SubItems(4).Text = pb.NgayNChuc
                    Else
                        MessageBox.Show("Sai dữ liệu nhập vào", "Thông báo", MessageBoxButtons.OK)
                    End If
                End If
            Next
            Refesh_()
        Else
            MessageBox.Show("Chưa Chọn Đối Tượng Cập Nhật", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_xoa.Click
        If status = True Then
            For Each dr As DataRow In data.Rows
                If dr("MaPB") = dlieu Then
                    dr.Delete() 'xoa du lieu tai dong so sánh
                    lvphongban.Items.RemoveAt(vt)
                    Exit For
                End If
            Next
            Refesh_()
        Else
            MessageBox.Show("Chưa Chọn Đối Tượng Cập Nhật", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_lamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_lamlai.Click
        Refesh_()
    End Sub

    Private Sub lvphongban_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvphongban.SelectedIndexChanged
        Try
            xlpb = New XLPhongBan
            vt = lvphongban.SelectedItems(0).Index()
            tb_mapb.Text = lvphongban.SelectedItems(0).SubItems(1).Text
            dlieu = lvphongban.SelectedItems(0).SubItems(1).Text
            tb_tenphong.Text = lvphongban.SelectedItems(0).SubItems(2).Text
            tb_truongphong.Text = xlpb.TimTenNV(lvphongban.SelectedItems(0).SubItems(3).Text, dsnv)
            tb_NChuc.Text = lvphongban.SelectedItems(0).SubItems(4).Text
            status = True
        Catch ex As Exception
        End Try
    End Sub

    Private Sub lb_tim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lb_tim.Click
        Dim nv As New NhanVien2
        st = True
        nv.Tag = tb_truongphong
        nv.ShowDialog()
    End Sub

    Private Sub PhongBan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        xlpb = New XLPhongBan
        data = xlpb.LayDSPB()
        dsnv = xlpb.LayDSNV()
        If data.Rows.Count < 0 Then Return
        lvphongban.Items.Clear()
        lvphongban.View = View.Details
        lvphongban.GridLines = True
        lvphongban.FullRowSelect = True
        For Each dr As DataRow In data.Rows
            dh(dr)
        Next
    End Sub
    Private Sub dh(ByVal dr As DataRow)
        Dim item As New ListViewItem
        item.Text = "STT"
        item.SubItems.Add(dr("MaPB"))
        item.SubItems.Add(dr("TenPB"))
        item.SubItems.Add(Convert.ToString(dr("MaTRP")))
        item.SubItems.Add(Convert.ToString(dr("NgayNC")))
        lvphongban.Items.Add(item)
        capnhatSTT()
    End Sub
    Private Sub capnhatSTT()
        For i As Integer = 1 To lvphongban.Items.Count
            lvphongban.Items(i - 1).Text = i.ToString
        Next
    End Sub

    Private Sub PhongBan_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            xlpb = New XLPhongBan
            If xlpb.SavePB(data) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub
    Private Sub Refesh_()
        tb_mapb.Text = String.Empty
        tb_tenphong.Text = String.Empty
        tb_truongphong.Text = String.Empty
        tb_NChuc.Text = String.Empty
        dlieu = String.Empty
        status = False
        manv = String.Empty
    End Sub

    Private Sub tb_truongphong_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_truongphong.Leave
        xlpb = New XLPhongBan
        If xlpb.TimMaNV(tb_truongphong.Text, manv, dsnv) = False Then
            tb_truongphong.Text = String.Empty
            MessageBox.Show("Nhân viên không tồn tại", "Thông báo", MessageBoxButtons.OK)
        End If
    End Sub
    Private Function LayDLieu(ByRef pb As DTO_PhongBan.PhongBan, ByVal dau As Integer) As Boolean
        xlpb = New XLPhongBan
        pb.MaPB = tb_mapb.Text
        pb.TenPhong = tb_tenphong.Text
        pb.TruongPhong = manv
        pb.NgayNChuc = tb_NChuc.Text
        If manv = "" Then
            pb.NgayNChuc = ""
        End If
        Return xlpb.Ktrathem(pb, data, dau)
    End Function
End Class